package com.songtech.server.Calculation;

import com.songtech.server.Point.PosPoint;
import com.songtech.server.constant.Calculate;
import com.songtech.server.constant.enumConstant.AlertEnum;
import com.songtech.server.entry.PositionProtocal;
import com.songtech.server.utils.CalculationUtils;
import com.songtech.web.entry.SysPosElefence;

/**
 * Create By YINN on 2018/4/16 15:51
 * Description :
 */
public class EleFenceCalculation {

    private double presentLat;

    private double presentLon;

    private double oriLat;

    private double oriLon;

    private double centerLat;

    private double centerLon;

    //半径 米
    private int r;

    /**
     * Created by YINN on 2018/4/28 13:57.
     * @Description : 电子围栏算法构造函数
     * @param present 当前点
     * @param ori 上一点位
     * @param elefence 电子围栏对象
     * @Result :
     */
    public EleFenceCalculation(PositionProtocal present, PositionProtocal ori, SysPosElefence elefence) {

        this.presentLat = (double) present.getLatitude() / Calculate.LON_LAT_FACTOR;

        this.presentLon = (double) present.getLongitude() / Calculate.LON_LAT_FACTOR;

        this.oriLat = (double) ori.getLatitude() / Calculate.LON_LAT_FACTOR;

        this.oriLon = (double) ori.getLongitude() / Calculate.LON_LAT_FACTOR;

        this.centerLat = (double) elefence.getCenterLatitude() / Calculate.LON_LAT_FACTOR;

        this.centerLon = (double)elefence.getCenterLongitude() / Calculate.LON_LAT_FACTOR;

        this.r = elefence.getRadius();

    }

    /**
     * Created by YINN on 2018/4/28 13:57.
     * @Description : 电子围栏算法构造函数
     * @param present 当前点
     * @param ori 上一点位
     * @param elefence 电子围栏对象
     * @Result :
     */
    public EleFenceCalculation(PosPoint present, PosPoint ori, SysPosElefence elefence) {

        this.presentLat =  present.getLat() / Calculate.LON_LAT_FACTOR;

        this.presentLon =  present.getLon() / Calculate.LON_LAT_FACTOR;

        this.oriLat =  ori.getLat() / Calculate.LON_LAT_FACTOR;

        this.oriLon =  ori.getLon() / Calculate.LON_LAT_FACTOR;

        this.centerLat = (double) (elefence.getCenterLatitude() / Calculate.LON_LAT_FACTOR);

        this.centerLon = (double) (elefence.getCenterLongitude() / Calculate.LON_LAT_FACTOR);

        this.r = elefence.getRadius();

    }

    public EleFenceCalculation(double presentLat, double presentLon, double oriLat, double oriLon, double centerLat, double centerLon, int r) {
        this.presentLat = presentLat;
        this.presentLon = presentLon;
        this.oriLat = oriLat;
        this.oriLon = oriLon;
        this.centerLat = centerLat;
        this.centerLon = centerLon;
        this.r = r;
    }

    public AlertEnum cal() {

        double oriR =  CalculationUtils.getDistance(oriLat, oriLon, centerLat, centerLon);

        double presentR =  CalculationUtils.getDistance(presentLat, presentLon, centerLat, centerLon);

        //原位置大于半径，新位置小于半径 入围栏
        //原位置小于半径，新位置大于半径 出围栏
        if (oriR > r && presentR < r){

            //入围栏
            return AlertEnum.IN_ELEFENCE;

        }else if (oriR < r && presentR > r){

            //出围栏
            return AlertEnum.OUT_ELEFENCE;
        }


        return null;
    }
}
